// Copyright(C) Japan Android Group Gunma
// LGPL License
// Writers Rabbit-U

package jp.android_group.gunma.gunma_app_lib;

import java.util.Map;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class RSSActivity extends Activity {

	static final String[] m_from = { CityInfo.m_keyName };
	static final int[] m_to = { android.R.id.text1 };
	
	Toast m_toast;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twitter);
		
		String city;
		
		{
			Intent intent = getIntent();
			city = intent.getStringExtra("city");
		}
		
		{
			CityInfo info = CityInfo.getCityInfo(city);
			m_adapter = new SimpleAdapter(this, info.m_listRSSUri, android.R.layout.simple_list_item_1, m_from, m_to);
		}
		
		{
			ListView v = (ListView)findViewById(R.id.ListViewHashTag);
			v.setAdapter(m_adapter);
			v.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View item, int pos, long id) {
					
					RSSActivity.this.onItemClick(pos);
					
				}
				
			});
		}
		
	}
	
	SimpleAdapter m_adapter;
	
	void onItemClick(int pos) {

		Uri uri;
		
		{
			Map<String, String> data = (Map<String, String>) m_adapter.getItem(pos);
			String strUri = data.get(CityInfo.m_keyUri);
			uri = Uri.parse(strUri);
		}
		
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setType("application/rss+xml");
		intent.putExtra(Intent.EXTRA_STREAM, uri);
		
		try {
			startActivity(intent);
		} catch ( ActivityNotFoundException e ) {
			m_toast = Toast.makeText(this, "RSS View not found.", Toast.LENGTH_SHORT);
			m_toast.show();
		}

	}

}
